public virtual BusinessResponse <int> Edit(TDto dto) { var businessResp = new BusinessResponse <int> { ResponseCode = ResponseCode.Fail }; var entity = Repository.GetById(dto.Id); if (entity == null) { businessResp.ResponseMessage = ErrorMessage.RecordNotFound; return(businessResp); } var type = typeof(TEntity); var entityProperties = type.GetProperties(); foreach (PropertyInfo entityProperty in entityProperties) { //Only modify settable properties. Do not change CreatedAt property. if (entityProperty.CanWrite && entityProperty.Name != "CreatedAt") { PropertyInfo dtoProperty = typeof(TDto).GetProperty(entityProperty.Name); //POCO obj must have same prop as model var value = dtoProperty.GetValue(dto); //get new value of entity from dto object entityProperty.SetValue(entity, value, null); //set new value of entity } } Repository.Update(entity); var affectedRows = Uow.Save(); //log db record modification as an info LogService.LogInfo(LogType.Modify, entity.Id, string.Format("'{0}' entity has been modified.", type.ToString())); businessResp.ResponseData = affectedRows; businessResp.ResponseCode = ResponseCode.Success; return(businessResp); }
public virtual BusinessResponse <IEnumerable <TDto> > GetAll() { var businessResp = new BusinessResponse <IEnumerable <TDto> > { ResponseCode = ResponseCode.Fail }; var entities = Repository.GetAll(); if (!entities.Any()) { businessResp.ResponseMessage = ErrorMessage.RecordNotFound; return(businessResp); } var dtos = Mapper.Map <IEnumerable <TEntity>, IEnumerable <TDto> >(entities); businessResp.ResponseCode = ResponseCode.Success; businessResp.ResponseData = dtos; return(businessResp); }
public virtual BusinessResponse <TDto> Get(int id) { var businessResp = new BusinessResponse <TDto> { ResponseCode = ResponseCode.Fail }; var entity = Repository.GetById(id); if (entity == null) { businessResp.ResponseMessage = ErrorMessage.RecordNotFound; return(businessResp); } var dto = Mapper.Map <TEntity, TDto>(entity); businessResp.ResponseCode = ResponseCode.Success; businessResp.ResponseData = dto; return(businessResp); }