public HttpResponseMessage AddWorkNoAuthentication(WorkCreateViewModel work) { try { Work newWork = new Work(); newWork.Body = work.Body; newWork.Genre = work.Genre; newWork.CoverPhoto = work.CoverPhoto; newWork.Title = work.Title; using (ApplicationDbContext db = new ApplicationDbContext()) { db.Works.Add(newWork); db.SaveChanges(); return(new HttpResponseMessage() { Content = new JsonContent(new { Success = true, data = newWork.Id }) }); } } catch (Exception ex) { return(new HttpResponseMessage() { Content = new JsonContent(new { Success = false, Message = ex.Message }) }); } }
public HttpResponseMessage CreateWork([FromBody] WorkCreateViewModel work, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string token) { if (ValidateToken(token)) { var currentUser = GetUserByToken(token); using (ApplicationDbContext db = new ApplicationDbContext()) { Work workDb = new Work(); workDb.UserId = currentUser.Id; workDb.CoverPhoto = work.CoverPhoto; workDb.Body = work.Body; workDb.Title = work.Title; workDb.Genre = work.Genre; workDb.Description = work.Description; for (int i = 0; i < work.Tags.Count; i++) { string value = work.Tags[i]; var dbTag = db.Tags.FirstOrDefault(s => s.Value == value.ToLower()); if (dbTag != null) { workDb.Tags.Add(dbTag); } else { Tag tag = new Tag(); tag.Value = value.ToLower(); workDb.Tags.Add(tag); } } db.Works.Add(workDb); db.SaveChanges(); return(new HttpResponseMessage() { Content = new JsonContent(new { Success = true, Data = workDb.Id }) }); } } else { return(new HttpResponseMessage() { Content = new JsonContent(new { Success = false, Message = "Authentication failed!" }) }); } }
public async Task <IActionResult> Add([FromBody] WorkCreateViewModel item) { var id = await _commandFunctionality.AddAsync(Mapper.Map <WorkCreateCommand>(item)); return(ResponseWithData(StatusCodes.Status201Created, id)); }