public async Task <ActionResult <ProductSessionDto> > Put([FromBody] ProductSessionEditDto value) { if (value.Id == Guid.Empty) { throw new Exception("Unable to edit a ProductSession without ID"); } var res = await _service.Save(value); return(res); }
public static ProductSession ToEntity(this ProductSessionEditDto e) { if (e == null) { return(null); } var res = new ProductSession(); res.Id = e.Id; res.Description = e.Description; res.IdTeacher = e.IdTeacher; res.IdProduct = e.IdProduct; return(res); }
public async Task <ProductSessionDto> Save(ProductSessionEditDto itemToEdit) { ProductSession res; if (itemToEdit.Id != Guid.Empty) { _logger.LogDebug($"Calling Update ProductSession for id=[{itemToEdit.Id}]"); //edit res = await this.GetInner(itemToEdit.Id); if (res == null) { throw new NotFoundException($"ProductSession with id={itemToEdit.Id} not exists!"); } res.Description = itemToEdit.Description; res.IdTeacher = itemToEdit.IdTeacher; res.IdProduct = itemToEdit.IdProduct; _dbCtx.ProductSessions.Update(res); _dbCtx.SaveChanges(); } else { //insert res = itemToEdit.ToEntity(); res.Id = Guid.NewGuid(); var claims = ((ClaimsIdentity)_claimPrincipal.Identity).Claims; var userId = claims.SingleOrDefault(x => x.Type == ClaimTypes.Name); if (userId == null || string.IsNullOrEmpty(userId.Value)) { throw new Exception("Cannot find teacherId"); } res.IdTeacher = Guid.Parse(userId.Value); _logger.LogDebug($"Calling Insert ProductSession for id=[{res.Id}] (temp id, not created yet!)"); await _dbCtx.ProductSessions.AddAsync(res); _dbCtx.SaveChanges(); await AddSubscribers(res.IdProduct, res.Id); } return(res.ToDto()); }
public async Task <ActionResult <ProductSessionDto> > Post([FromBody] ProductSessionEditDto value) { var res = await _service.Save(value); return(res); }