using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using EnumsNET; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using AyaNova.Models; namespace AyaNova.Biz { internal class UserOptionsBiz : BizObject { internal UserOptionsBiz(AyContext dbcontext, long currentUserId, AuthorizationRoles userRoles) { ct = dbcontext; UserId = currentUserId; CurrentUserRoles = userRoles; BizType = AyaType.UserOptions; } //////////////////////////////////////////////////////////////////////////////////////////////// /// GET //Get one internal async Task GetAsync(long fetchId) { //NOTE: get by UserId as there is a 1:1 relationship, not by useroptions id //This is simple so nothing more here, but often will be copying to a different output object or some other ops return await ct.UserOptions.SingleOrDefaultAsync(z => z.UserId == fetchId); } //////////////////////////////////////////////////////////////////////////////////////////////// //UPDATE // //put internal async Task PutAsync(UserOptions dbObject, UserOptions inObj) { //Replace the db object with the PUT object CopyObject.Copy(inObj, dbObject, "Id, UserId"); //Set "original" value of concurrency token to input token //this will allow EF to check it out ct.Entry(dbObject).OriginalValues["Concurrency"] = inObj.Concurrency; Validate(dbObject); if (HasErrors) return false; await ct.SaveChangesAsync(); //Log await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, AyaType.User, AyaEvent.Modified), ct); return true; } //////////////////////////////////////////////////////////////////////////////////////////////// //VALIDATION // //Can save or update? private void Validate(UserOptions inObj) { //UserOptions is never new, it's created with the User object so were only here for an edit //UserId required if (inObj.UserId == 0) AddError(ApiErrorCode.VALIDATION_REQUIRED, "UserId"); if (inObj.UiColor.Length > 12) { AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "UiColor", "UiColor must be HEX color value"); } return; } ///////////////////////////////////////////////////////////////////// }//eoc }//eons