Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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());
        }
Example #4
0
        public async Task <ActionResult <ProductSessionDto> > Post([FromBody] ProductSessionEditDto value)
        {
            var res = await _service.Save(value);

            return(res);
        }