Made reading tickets from database.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent 9682a7acb4
commit 0e013af5a1

@ -7,5 +7,10 @@ namespace AbpDesk.Tickets.Dtos
public string Title { get; set; }
public string Body { get; set; }
public override string ToString()
{
return $"{base.ToString()}, Title = {Title}, Body = {Body}";
}
}
}

@ -1,17 +1,27 @@
using System.Collections.Generic;
using System.Linq;
using AbpDesk.Tickets.Dtos;
using Volo.Abp.Application.Services.Dtos;
using Volo.Abp.Domain.Repositories;
namespace AbpDesk.Tickets
{
public class TicketAppService : ITicketAppService
{
private readonly IRepository<Ticket, int> _ticketRepository;
public TicketAppService(IRepository<Ticket, int> ticketRepository)
{
_ticketRepository = ticketRepository;
}
public ListResultDto<TicketDto> GetAll()
{
return new ListResultDto<TicketDto>(new List<TicketDto>
{
new TicketDto {Id = 1, Title = "Ticket 1 Title", Body = "Ticket 1 Body" }
});
var tickets = _ticketRepository
.GetAllList()
.Select(t => new TicketDto { Id = t.Id, Title = t.Title, Body = t.Body })
.ToList();
return new ListResultDto<TicketDto>(tickets);
}
}
}

@ -11,9 +11,17 @@
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
},
"Microsoft.EntityFrameworkCore.Tools": {
"type": "build",
"version": "1.1.0-preview4-final"
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
},
"frameworks": {
"netcoreapp1.1": {
"imports": "dnxcore50"

@ -1,4 +1,7 @@
using Volo.Abp.Domain.Entities;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using Volo;
using Volo.Abp.Domain.Entities;
namespace AbpDesk.Tickets
{
@ -8,8 +11,11 @@ namespace AbpDesk.Tickets
public const int MaxBodyLength = 64 * 1024; //64K
[Required]
[MaxLength(MaxTitleLength)]
public string Title { get; set; }
[MaxLength(MaxBodyLength)]
public string Body { get; set; }
private Ticket()
@ -17,8 +23,10 @@ namespace AbpDesk.Tickets
}
public Ticket(string title, string body)
public Ticket([NotNull] string title, string body)
{
Check.NotNull(title, nameof(title));
Title = title;
Body = body;
}

@ -3,6 +3,7 @@
"dependencies": {
"NETStandard.Library": "1.6.1",
"System.ComponentModel.Annotations": "4.3.0",
"Volo.Abp": "1.0.0-*"
},

@ -1,13 +1,15 @@
using AbpDesk.Tickets;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace AbpDesk.EntityFrameworkCore
{
public class AbpDeskDbContext : DbContext
public class AbpDeskDbContext : AbpDbContext<AbpDeskDbContext>
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
public AbpDeskDbContext(DbContextOptions<AbpDeskDbContext> options)
: base(options)
{
optionsBuilder.UseSqlServer("Server=localhost;Database=AbpDesk;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

@ -1,14 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace AbpDesk.EntityFrameworkCore
{
[DependsOn(typeof(AbpDeskDomainModule))]
public class AbpDeskEntityFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddAssemblyOf<AbpDeskEntityFrameworkCoreModule>();
}
}
}

@ -0,0 +1,25 @@
using AbpDesk.Tickets;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.Repositories.EntityFrameworkCore;
namespace AbpDesk.EntityFrameworkCore
{
[DependsOn(typeof(AbpDeskDomainModule))]
public class AbpDeskEntityFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddAssemblyOf<AbpDeskEntityFrameworkCoreModule>();
services.AddTransient<IRepository<Ticket, int>, EfCoreRepository<AbpDeskDbContext, Ticket, int>>();
services.AddDbContext<AbpDeskDbContext>(options =>
{
options.UseSqlServer("Server=localhost;Database=AbpDesk;Trusted_Connection=True;");
});
}
}
}

@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using AbpDesk.EntityFrameworkCore;
namespace AbpDesk.EntityFrameworkCore.Migrations
{
[DbContext(typeof(AbpDeskDbContext))]
[Migration("20161211123930_Ticket_Added_Data_Annotations")]
partial class Ticket_Added_Data_Annotations
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "1.1.0-rtm-22752")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("AbpDesk.Tickets.Ticket", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Body")
.HasMaxLength(65536);
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(256);
b.HasKey("Id");
b.ToTable("DskTickets");
});
}
}
}

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
namespace AbpDesk.EntityFrameworkCore.Migrations
{
public partial class Ticket_Added_Data_Annotations : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Title",
table: "DskTickets",
maxLength: 256,
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Title",
table: "DskTickets",
nullable: true,
oldClrType: typeof(string),
oldMaxLength: 256);
}
}
}

@ -21,9 +21,12 @@ namespace AbpDesk.EntityFrameworkCore.Migrations
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Body");
b.Property<string>("Body")
.HasMaxLength(65536);
b.Property<string>("Title");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(256);
b.HasKey("Id");

@ -4,18 +4,17 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>a1ae63e9-0cf4-4afb-a584-65d826dea3cb</ProjectGuid>
<RootNamespace>Volo.Abp.EntityFrameworkCore</RootNamespace>
<RootNamespace>
</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Volo.Abp.EntityFrameworkCore
{
public class AbpDbContext<TDbContext> : DbContext
where TDbContext : DbContext
{
public AbpDbContext(DbContextOptions<TDbContext> options)
: base(options)
{
}
}
}

@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore;
namespace Volo.Abp.Repositories.EntityFrameworkCore
{
//TODO: Override async versions
public class EfCoreRepository<TDbContext, TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey>
where TDbContext : AbpDbContext<TDbContext>
where TEntity : class, IEntity<TPrimaryKey>
{
protected virtual TDbContext DbContext { get; }
protected virtual DbSet<TEntity> DbSet => DbContext.Set<TEntity>();
public EfCoreRepository(TDbContext dbContext)
{
DbContext = dbContext;
}
public override List<TEntity> GetAllList()
{
return GetQueryable().ToList();
}
public override Task<List<TEntity>> GetAllListAsync()
{
return GetQueryable().ToListAsync();
}
public override TEntity Get(TPrimaryKey id)
{
var entity = FirstOrDefault(id);
if (entity == null)
{
throw new EntityNotFoundException(typeof(TEntity), id);
}
return entity;
}
public override TEntity FirstOrDefault(TPrimaryKey id)
{
return DbSet.Find(id);
}
public override TEntity Insert(TEntity entity)
{
return DbSet.Add(entity).Entity;
}
public override TEntity Update(TEntity entity)
{
return DbSet.Update(entity).Entity;
}
public override void Delete(TEntity entity)
{
DbSet.Remove(entity);
}
public override void Delete(TPrimaryKey id)
{
var entity = FirstOrDefault(id);
if (entity == null)
{
return;
}
Delete(entity);
}
public override int Count()
{
return GetQueryable().Count();
}
protected virtual IQueryable<TEntity> GetQueryable()
{
return DbSet;
}
}
}

@ -0,0 +1,78 @@
using System;
namespace Volo.Abp.Domain.Entities
{
/// <summary>
/// This exception is thrown if an entity excepted to be found but not found.
/// </summary>
//[Serializable]
public class EntityNotFoundException : AbpException
{
/// <summary>
/// Type of the entity.
/// </summary>
public Type EntityType { get; set; }
/// <summary>
/// Id of the Entity.
/// </summary>
public object Id { get; set; }
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
public EntityNotFoundException()
{
}
///// <summary>
///// Creates a new <see cref="EntityNotFoundException"/> object.
///// </summary>
//public EntityNotFoundException(SerializationInfo serializationInfo, StreamingContext context)
// : base(serializationInfo, context)
//{
//}
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
public EntityNotFoundException(Type entityType, object id)
: this(entityType, id, null)
{
}
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
public EntityNotFoundException(Type entityType, object id, Exception innerException)
: base($"There is no such an entity. Entity type: {entityType.FullName}, id: {id}", innerException)
{
EntityType = entityType;
Id = id;
}
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
public EntityNotFoundException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
/// <param name="innerException">Inner exception</param>
public EntityNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
@ -74,5 +76,17 @@ namespace Volo.Abp.Domain.Repositories
{
return Task.FromResult(Count());
}
protected static Expression<Func<TEntity, bool>> CreateEqualityExpressionForId(TPrimaryKey id)
{
var lambdaParam = Expression.Parameter(typeof(TEntity));
var lambdaBody = Expression.Equal(
Expression.PropertyOrField(lambdaParam, "Id"),
Expression.Constant(id, typeof(TPrimaryKey))
);
return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam);
}
}
}

Loading…
Cancel
Save