Beispiel #1
0
        public async Task <ActionResult <User> > Update([FromRoute] Guid id, [FromBody] Dto.Update.Request requestData)
        {
            try
            {
                var user = await _dataContext.Users
                           .FirstAsync(u => u.Id == id);

                if (requestData.SubjectId != null &&
                    await _dataContext.Subjects.CountAsync(u => u.Id == requestData.SubjectId) == 0)
                {
                    return(new BadRequestResult());
                }

                user.FirstName  = requestData.FirstName;
                user.LastName   = requestData.LastName;
                user.Biography  = requestData.Biography;
                user.HourlyRate = requestData.HourlyRate;
                user.Currency   = requestData.Currency;
                user.SubjectId  = requestData.SubjectId;

                await _dataContext.SaveChangesAsync();

                return(user);
            }
            catch (InvalidOperationException)
            {
                return(new NotFoundResult());
            }
            catch (ArgumentNullException)
            {
                return(new NotFoundResult());
            }
        }
Beispiel #2
0
        public async Task <ActionResult <User> > SelfUpdate([FromBody] Dto.Update.Request requestData)
        {
            try
            {
                var email = User.Claims.First(claim => claim.Type == ClaimTypes.Email).Value;

                var user = await _dataContext.Users
                           .FirstAsync(u => u.Email == email);

                if (requestData.SubjectId != null &&
                    await _dataContext.Subjects.CountAsync(u => u.Id == requestData.SubjectId) == 0)
                {
                    return(new BadRequestResult());
                }

                user.FirstName  = requestData.FirstName;
                user.LastName   = requestData.LastName;
                user.Biography  = requestData.Biography;
                user.HourlyRate = requestData.HourlyRate;
                user.Currency   = requestData.Currency;
                user.SubjectId  = requestData.SubjectId;
                user.IsActive   = requestData.IsActive;

                await _dataContext.SaveChangesAsync();

                return(user);
            }
            catch (InvalidOperationException)
            {
                return(new NotFoundResult());
            }
            catch (ArgumentNullException)
            {
                return(new NotFoundResult());
            }
        }