public IHttpActionResult PostGlnTagType(GlnTagTypeDto glnTagTypeDto) { if (!ModelState.IsValid) { _logger.FailedToCreateServerLog(HttpContext.Current.User, "Unable to create new tag type, state invalid.", "", glnTagTypeDto); return(BadRequest(ModelState)); } var sameDescription = _unitOfWork.GlnTagType.Find(tt => tt.Description == glnTagTypeDto.Description); var sameCode = _unitOfWork.GlnTagType.Find(tt => tt.Code == glnTagTypeDto.Code); if (sameDescription.Any()) { _logger.FailedToCreateServerLog(HttpContext.Current.User, $"A GlnTagType already exists with this decription: {glnTagTypeDto.Description}", "", glnTagTypeDto); return(BadRequest($"A GlnTagType already exists with this decription: {glnTagTypeDto.Description}")); } if (sameCode.Any()) { _logger.FailedToCreateServerLog(HttpContext.Current.User, $"A GlnTagType already exists with this code: {glnTagTypeDto.Code}", "", glnTagTypeDto); return(BadRequest($"A GlnTagType already exists with this code: {glnTagTypeDto.Code}")); } var glnTagType = new GlnTagType() { Active = glnTagTypeDto.Active, Code = glnTagTypeDto.Code, CreatedDateTime = DateTime.Now, Description = glnTagTypeDto.Description, ModifiedDateTime = DateTime.Now, UserCreated = HttpContext.Current.User.ToString() }; _unitOfWork.GlnTagType.Add(glnTagType); try { _unitOfWork.Complete(); _logger.SuccessfullyAddedServerLog(HttpContext.Current.User, DtoHelper.CreateGlnTagTypeDto(glnTagType)); } catch (Exception e) { Console.WriteLine(e); _logger.FailedToCreateServerLog(HttpContext.Current.User, e.Message, e.InnerException, glnTagTypeDto); return(InternalServerError()); } return(Ok(DtoHelper.CreateGlnTagTypeDto(glnTagType))); }
public static GlnTagTypeDto CreateGlnTagTypeDto(GlnTagType glnTagType) { var tagTypeDto = new GlnTagTypeDto() { GlnTagTypeId = glnTagType.GlnTagTypeId, Code = glnTagType.Code, Description = glnTagType.Description, Active = glnTagType.Active }; //tagTypeDto.GlnTags = glnTagType.GlnTags?.Select(t => new GlnTagDto() //{ // Active = t.Active, // GlnId = t.GlnId, // GlnTagTypeId = t.GlnTagTypeId, // GlnTagId = t.GlnTagId, // TypeKey = t.TypeKey //}).ToList(); return(tagTypeDto); }
public IHttpActionResult PutGlnTagType(GlnTagTypeDto glnTagType) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var tagTypeToBeUpdated = _unitOfWork.GlnTagType.FindSingle(tt => tt.GlnTagTypeId == glnTagType.GlnTagTypeId); if (Equals(tagTypeToBeUpdated, null)) { return(NotFound()); } var beforeUpdate = DtoHelper.CreateGlnTagTypeDto(tagTypeToBeUpdated); tagTypeToBeUpdated.Code = glnTagType.Code; tagTypeToBeUpdated.Description = glnTagType.Description; tagTypeToBeUpdated.ModifiedDateTime = DateTime.Now; tagTypeToBeUpdated.UserModified = HttpContext.Current.User.ToString(); tagTypeToBeUpdated.Active = glnTagType.Active; _unitOfWork.GlnTagType.Update(tagTypeToBeUpdated); try { _unitOfWork.Complete(); _logger.SuccessfulUpdateServerLog(HttpContext.Current.User, beforeUpdate, DtoHelper.CreateGlnTagTypeDto(tagTypeToBeUpdated)); } catch (DbUpdateConcurrencyException) { _logger.FailedToCreateServerLog(HttpContext.Current.User, "Unable to create new tag type", "", DtoHelper.CreateGlnTagTypeDto(tagTypeToBeUpdated)); return(InternalServerError()); } return(Ok(DtoHelper.CreateGlnTagTypeDto(tagTypeToBeUpdated))); //return StatusCode(HttpStatusCode.NoContent); }