public async Task <dynamic> Create(Article article) { try { if (article != null) { article.Createddate = DateTime.UtcNow; article.Lastmodifieddate = DateTime.UtcNow; //get the user from the session and assign that user in createdbyid int userId = Convert.ToInt32(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); article.Createdbyid = userId; _dbContext.Articles.Add(article); article.Tags.ToList().ForEach(x => { x.Lastmodifieddate = DateTime.UtcNow; }); _dbContext.SaveChanges(); return(await System.Threading.Tasks.Task.FromResult(article)); } return(null); } catch (Exception ex) { ex = ex.InnerException != null ? ex.InnerException : ex; var response = new ErrorResponse { errorMessage = ex.Message }; return(response); } }
public async Task <dynamic> Create(Userdetail user) { if (user != null) { //validate for duplicate username if (dbContext.Userdetails.Where(x => x.Username == user.Username).Any()) { throw new Exception(string.Format(ValidationMessages.DUPLICATE, PropertyNames.USER)); } //add lastmodified date user.Lastmodifieddate = DateTime.UtcNow; //add the user to the db and save changes dbContext.Add(user); dbContext.SaveChanges(); return(await System.Threading.Tasks.Task.FromResult(user)); } return(null); }