<commit_before>﻿using System;
using System.Web.Http;
using Agiil.Auth;
using Agiil.Domain.Auth;
using Agiil.Web.Models;

namespace Agiil.Web.Controllers
{
  [AllowAnonymous]
  public class AddUserController : ApiController
  {
    readonly IUserCreator userCreator;
    readonly IGetsUserByUsername userQuery;
    readonly IUserPasswordUpdater passwordChanger;

    public IHttpActionResult Post(CreateUserModel model)
    {
      if(model == null)
        throw new ArgumentNullException(nameof(model));

      bool exists;

      var existingUser = userQuery.Get(model.username);
      if(existingUser == null)
      {
        userCreator.Add(model.username, model.password);
        exists = false;
      }
      else
      {
        passwordChanger.ChangePassword(existingUser, model.password);
        exists = true;
      }
      
      return Ok(exists? $"The existing user '{model.username}' was updated" : $"The user '{model.username}' was created");
    }

    public AddUserController(IUserCreator userCreator,
                             IGetsUserByUsername userQuery,
                             IUserPasswordUpdater passwordChanger)
    {
      if(passwordChanger == null)
        throw new ArgumentNullException(nameof(passwordChanger));
      if(userQuery == null)
        throw new ArgumentNullException(nameof(userQuery));
      if(userCreator == null)
        throw new ArgumentNullException(nameof(userCreator));
      
      this.userCreator = userCreator;
      this.passwordChanger = passwordChanger;
      this.userQuery = userQuery;
    }
  }
}<commit_msg>WIP #58 - Add transaction so that service actually does something<commit_after>﻿using System;
using System.Web.Http;
using Agiil.Auth;
using Agiil.Domain.Auth;
using Agiil.Web.Models;
using CSF.Data;

namespace Agiil.Web.Controllers
{
  [AllowAnonymous]
  public class AddUserController : ApiController
  {
    readonly IUserCreator userCreator;
    readonly IGetsUserByUsername userQuery;
    readonly IUserPasswordUpdater passwordChanger;
    readonly ITransactionCreator transactionCreator;

    public IHttpActionResult Post(CreateUserModel model)
    {
      if(model == null)
        throw new ArgumentNullException(nameof(model));

      bool exists;

      using(var tran = transactionCreator.BeginTransaction())
      {
        var existingUser = userQuery.Get(model.username);

        if(existingUser == null)
        {
          userCreator.Add(model.username, model.password);
          exists = false;
        }
        else
        {
          passwordChanger.ChangePassword(existingUser, model.password);
          exists = true;
        }

        tran.Commit();
      }

      return Ok(exists? $"The existing user '{model.username}' was updated" : $"The user '{model.username}' was created");
    }

    public AddUserController(IUserCreator userCreator,
                             IGetsUserByUsername userQuery,
                             IUserPasswordUpdater passwordChanger,
                             ITransactionCreator transactionCreator)
    {
      if(transactionCreator == null)
        throw new ArgumentNullException(nameof(transactionCreator));
      if(passwordChanger == null)
        throw new ArgumentNullException(nameof(passwordChanger));
      if(userQuery == null)
        throw new ArgumentNullException(nameof(userQuery));
      if(userCreator == null)
        throw new ArgumentNullException(nameof(userCreator));
      
      this.userCreator = userCreator;
      this.passwordChanger = passwordChanger;
      this.userQuery = userQuery;
      this.transactionCreator = transactionCreator;
    }
  }
}
{'fraction_non_alphanumeric': 0.08312689237544729, 'fraction_numerical': 0.0005505092210294523, 'mean_word_length': 2.7386831275720165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1084811', 'n_tokens_mistral': 995, 'n_tokens_neox': 983, 'n_words': 211}