예제 #1
0
        public async Task <IActionResult> Create([FromBody] SchemeDto model)
        {
            var contributor = await GetContributor(User);

            // var searchResult = await _dbSet
            //     .FirstOrDefaultAsync(s => s.Id == model.Id);

            var scheme = new Scheme
            {
                Title       = model.Title,
                Description = model.Description,
                Content     = model.Content,
                Author      = contributor,
                Category    = _context.Set <Category>().Single(c => c.Id == model.Category),
                AgeStart    = model.AgeStart,
                AgeEnd      = model.AgeEnd
            };

            await addPlacesToScheme(model.Places, scheme, contributor);

            var schemeResult = await _dbSet.AddAsync(scheme);

            var result = await _context.SaveChangesAsync();

            return(base.ReturnResult(new SchemeDto(scheme)
            {
                IsFavorite = false,
                AuthorName = $"{contributor.Firstname} {contributor.Lastname}",
                Category = scheme.CategoryId,
                CategoryName = scheme.Category.Name,
                Places = scheme.SchemeSelectedPlaces.Where(sp => sp.Scheme.Id == scheme.Id).Select(sp => sp.Place.Id).ToList(),
            }));
        }
예제 #2
0
        public async Task <IActionResult> Update([FromBody] SchemeDto model)
        {
            var contributor = await GetContributor(User);

            // var searchResult = await _dbSet
            //     .FirstOrDefaultAsync(s => s.Id == model.Id);
            var scheme = new Scheme
            {
                Id          = model.Id,
                Title       = model.Title,
                Description = model.Description,
                Content     = model.Content,
                Author      = contributor,
                Category    = _context.Set <Category>().Single(c => c.Id == model.Category),
                AgeStart    = model.AgeStart,
                AgeEnd      = model.AgeEnd
            };

            try
            {
                _context.Update(scheme);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.)
            }

            return(base.ReturnResult(new SchemeDto(scheme)
            {
                IsFavorite = false,
                AuthorName = $"{contributor.Firstname} {contributor.Lastname}",
                Category = scheme.CategoryId,
                CategoryName = scheme.Category.Name,
            }));
        }