using AutoMapper; using Complex.Common.Dto; using Complex.Domain.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Complex.Application.Basic { public interface IBasicService { ResultDto> UnitStateList(Guid ComplexId); ResultDto InsertUnitState(UnitStateDto unitState); ResultDto EditUnitState(UnitStateDto unitState); } public class BasicService : IBasicService { private readonly IComplexDBContext _complexDBContext; private readonly IMapper _mapper; public BasicService(IComplexDBContext complexDBContext, IMapper mapper) { _complexDBContext = complexDBContext; _mapper = mapper; } public ResultDto> UnitStateList(Guid ComplexId) { var UnitStates = _complexDBContext.UnitStates.Where(s => s.ComplexId == ComplexId || !s.ComplexId.HasValue).ToList() .Select(u=>_mapper.Map(u)).ToList(); return new ResultDto> { Data = UnitStates, IsSuccess = true, RowCount = UnitStates.Count }; } public ResultDto EditUnitState(UnitStateDto unitStateDto) { var unitState = _complexDBContext.UnitStates.FirstOrDefault(us => us.Id == unitStateDto.Id); unitState.Title = unitStateDto.Title; unitState.Icon= unitStateDto.Icon; _complexDBContext.SaveChanges(); return new ResultDto { IsSuccess = true, Message="ویرایش با موفقیت انجام شد"}; } public ResultDto InsertUnitState(UnitStateDto unitStateDto) { var newUnitState = new UnitState(); newUnitState.Title= unitStateDto.Title; newUnitState.Icon= unitStateDto.Icon; newUnitState.ComplexId= unitStateDto.ComplexId; _complexDBContext.UnitStates.Add(newUnitState); _complexDBContext.SaveChanges(); return new ResultDto { IsSuccess = true, Data= newUnitState.Id, Message = "ثبت با موفقیت انجام شد" }; } } public class UnitStateDto { public int Id { get; set; } public Guid? ComplexId { set; get; } public string Title { set; get; } public string Icon { set; get; } } }