public async Task <ActionResult <ActivityType> > PostActivityType(ActivityTypeDto activityTypeDto, [FromRoute] string userId)
        {
            var activityType = new ActivityType();

            activityType.ActivityTypeName = activityTypeDto.ActivityTypeName;

            _context.ActivityType.Add(activityType);
            await _context.SaveChangesAsync();

            var userActivityType = new UserActivityType();

            userActivityType.TimeFrom = activityTypeDto.TimeFrom;
            userActivityType.TimeTo   = activityTypeDto.TimeTo;

            userActivityType.ActivityType   = activityType;
            userActivityType.ActivityTypeId = activityType.ActivityTypeId;

            var user = _context.User.Where(r => r.UserId == Int32.Parse(userId)).FirstOrDefault();

            userActivityType.User   = user;
            userActivityType.UserId = user.UserId;

            _context.UserActivityType.Add(userActivityType);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetActivityType", new { userId = userId, id = activityType.ActivityTypeId }, activityType));
        }
        public async Task <IActionResult> PutActivityType(int id, ActivityTypeDto activityTypeDto, [FromRoute] string userId)
        {
            var activityType = _context.ActivityType.Include(a => a.UserActivityType).Include(p => p.Activity)
                               .ThenInclude(i => i.ActivityTask).ThenInclude(p => p.Schedule).Where(a => a.ActivityTypeId == id).FirstOrDefault();

            activityType.ActivityTypeName = activityTypeDto.ActivityTypeName;

            _context.Entry(activityType).State = EntityState.Modified;

            var userActivityType = _context.UserActivityType.Include(i => i.ActivityType).Include(o => o.User)
                                   .Where(o => o.UserId == Int32.Parse(userId) && o.ActivityTypeId == activityType.ActivityTypeId).FirstOrDefault();

            userActivityType.TimeFrom = activityTypeDto.TimeFrom;
            userActivityType.TimeTo   = activityTypeDto.TimeTo;

            _context.Entry(userActivityType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ActivityTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 3
0
 public static ActivityTypeModel Map(ActivityTypeDto activityTypeDto)
 {
     return(new ActivityTypeModel
     {
         Id = activityTypeDto.Id,
         Name = activityTypeDto.Name
     });
 }